
#include <network/ServidorHTTP.h>


#define DEFAULT_BUFLEN 512
#define TAM_BUF 512
#define ENTER_2 "\15\12\15\12"

#include <iostream>
#include <string>



ServidorHTTP::ServidorHTTP(int porta, int backlog)
{
    sserv = new Socket;
    
    sserv->bind(porta);
    
    sserv->listen(5);
    
    printf("Escutando na porta %u...\n\n", sserv->getPort());
}


ServidorHTTP::~ServidorHTTP()
{
    sserv->close();
}


RequestHTTP ServidorHTTP::RecebeRequisicao()
{
    sc = sserv->accept();
    
    if(!sc)
        return RequestHTTP();

    std::vector<char> temp;

    size_t tam;
    char buf[1024] = {0};

#ifdef _MSC_VER
    strcpy_s(buf, 1024, "buf");
#else
    strcpy(buf, "buf");
#endif

	while  ((strstr(buf, "\r\n") == 0) &&
            (strlen(buf)          > 0)   )
    {
        std::vector<char> bufv = sc->recv(/*Socket::MAX_BUFFER*/);
        tam = bufv.size();
        for(unsigned i=0; i<bufv.size(); i++)
        {
            buf[i]=bufv[i];
        }
        temp.insert(temp.end(), bufv.begin(), bufv.end());
        //printf(buf);
    }
    
    // Decodifica
    std::string strCabecalho(temp.begin(), temp.end());


    RequestHTTP req;
    
    req.strIP = sc->getIP();
    
    
    while(strCabecalho.size())
    {
        size_t dist = strCabecalho.find("\r\n");
        std::string strLinha = strCabecalho.substr(0, dist);
        if(strLinha.size() == 0)
            break;
        strCabecalho = strCabecalho.substr(dist+2);
        //std::cout << "+" <<  strLinha << "+" << std::endl;
        
        size_t fim_param = strLinha.find_first_of(": ");
        std::string strParam = strLinha.substr(0, fim_param);
        std::string strValor;
        if(strParam == "GET")
        {
            size_t ini_valor = strLinha.find_first_not_of(" ",3);
            size_t fim_valor = strLinha.find_first_of(" ",ini_valor);
            strValor = strLinha.substr(ini_valor, fim_valor-ini_valor);
        }
        else
        {
            size_t ini_valor = strLinha.find_first_of(":");
            ini_valor = strLinha.find_first_not_of(" ", ini_valor+1);
            strValor = strLinha.substr(ini_valor);
        }
        //std::cout << "Param='" <<  strParam << "'     Valor='" << strValor << "'" << std::endl << std::endl;
        
        

        if(strParam == "GET")
            req.strFile = strValor;
        if(strParam == "Accept")
            req.strAccept = strValor;
    }
    
    
    return req;
}

void ServidorHTTP::EnviaResposta(ResponseHTTP res)
{
    std::string cabecalho;
    
    cabecalho += "HTTP/1.1 "+res.strStatusCode+" "+res.strStatus+"\r\n";
    if(res.strContentType.size())
        cabecalho += "Content-Type: "+res.strContentType+"\r\n";
    size_t iContentLength;
    if(res.strContentLength.size())
    {
        iContentLength = atoi(res.strContentLength.c_str());
        cabecalho += "Content-Length: "+res.strContentLength+"\r\n";
    }
    else
    {
        char* bufff = new char[50000];
        iContentLength = res.conteudo.size();

#ifdef _MSC_VER
		sprintf_s(bufff, 50000, "Content-Length: %d\r\n",res.conteudo.size());
#else
		sprintf(bufff,"Content-Length: %d\r\n",res.conteudo.size());
#endif

		cabecalho += bufff;
		delete[] bufff;
    }
    cabecalho += "\r\n";
    
    //std::cout << cabecalho;
    
    std::vector<char> bufv(cabecalho.begin(), cabecalho.end());
    
    bufv.insert(bufv.end(), res.conteudo.begin(), res.conteudo.begin()+iContentLength);
/*
    for(int i=0; i<bufv.size(); i++)    
        putchar(bufv[i]);
*/

    sc->send(bufv);

    sc->close();
}
